package api

import (
	"github.com/labstack/echo"
	"net/http"
	"pwas/model"
)

// Input 输入
type Input struct {
	ID int
	Nickname string
	Password string
}

// Route 路由
func Route(e *echo.Echo)  {
	e.POST("/user", func(c echo.Context) error {
		return c.String(http.StatusOK, "erwfew")
	})

	e.POST("/user", func(c echo.Context) error {
		return c.String(http.StatusOK, "efew")
	})

	e.POST("/user/register", func(c echo.Context) error {
		i := new(Input)
		if err := c.Bind(i); err != nil {
			return c.String(http.StatusOK, err.Error())
		}
		_, err := model.Register(i.Nickname, i.Password)
		if err != nil {
			return c.String(http.StatusOK, "user model:" + err.Error())
		}
		return c.String(http.StatusOK, "ok")
	})

	e.POST("/user/login", func(c echo.Context) error {
		i := new(Input)
		if err := c.Bind(i); err != nil {
			return c.String(http.StatusOK, err.Error())
		}
		_, err := model.Login(i.ID, i.Password)
		if err != nil {
			return c.String(http.StatusOK, err.Error())
		}
		return c.String(http.StatusOK, "ok")
	})
}